<template>
  <div style="float: left;margin-top: 5px">
    <el-form ref="roleSearchRef" :model="searchFormData">
      <el-form-item label="关键词" >
        <el-input  type="text" v-model="searchFormData.name"></el-input>
      </el-form-item>
    </el-form>
  </div>
  <div style="height: 40px;margin-top: 5px;float: left;margin-left: 20px;">
    <el-button @click="searchDicClick">查询</el-button>
    <el-button @click="resetSearchClick">重置</el-button>
    <el-button v-check-permission="'system:role:add'" @click="addDicClick">新增</el-button>
  </div>
  <div style="clear: both"></div>
  <div style="height: calc(100% - 60px);">
    <Table  :show-page="true" ref="tableRef" :show-line-number="true"
            :showCheckBox="false" :url="roleUrl" :columns="columns">
      <template  v-slot:handle="slotProps">
        <el-button v-check-permission="'system:role:update'" @click.stop="editTableData(slotProps.rowData,slotProps.rowIndex)">编辑</el-button>
        <el-button v-check-permission="'system:role:delete'" @click.stop="deleteTableRowData(slotProps.rowData,slotProps.rowIndex)">删除</el-button>
      </template>
    </Table>
  </div>
  <DialogForm ref="roleEditFormRef" :form="roleForm" title="编辑角色" @validateSuccess="validateSuccess">
    <template v-slot:permissionTree="slotProps">
      <Tree ref="selectTree" :tree-data="menuTreeData" :checked-model="checkedModel" :options="menuTreeOptions" :data-config="dataConfig">
      </Tree>
    </template>
  </DialogForm>
</template>
<script setup >

import {inject, reactive, ref, toRaw} from "vue";
import Table from "@/components/plugs/table/Table.vue";
import {FormItemType} from "@/components/plugs/form/DialogFormInterface.js";
import DialogForm from "@/components/plugs/form/DialogForm.vue";
import {AXIOS_NAME} from '@/constant/constant.js'
import Tree from "@/components/plugs/tree/Tree.vue";
import {CheckedModel} from "@/components/plugs/tree/TreeView.js";
import {ElMessage} from "element-plus";
import {deleteRow, HandleEnum} from "@/utils/utils.ts";

const $axios = inject(AXIOS_NAME);
let menuTreeData = reactive([]);
const roleEditFormRef = ref(null);
const roleSearchRef = ref(null);
const tableRef = ref(null);
const selectTree = ref(null);
const roleUrl = ref("/role/getRolePage");
const checkedModel = CheckedModel.CASCADE;

const searchFormData = reactive({
  "name": ""
});
const dataConfig = {
  simple: true,
  pFiled:'pid',
  idField: 'id',
  rootId: '0'
};
const menuTreeOptions = {
  "label": 'name'
}
const columns = [
  {field:"name","desc": "名称"},
  {field:"status","desc": "状态",dicType:'start_using'},
  {field:"defaultStatus","desc": "是否默认",dicType:'role_default_status'},
  {field:"description","desc": "描述"},
  {field:"showTime","desc": "创建时间",width: 200},
  {field:"handle","desc": "操作",isSlot:true,width:200}
];

let items = [];
items.push({type: FormItemType.INPUT_HIDDEN,label: 'id',name: 'id'});
items.push({type: FormItemType.INPUT_TEXT,label: '名称',name: 'name'});
items.push({type: FormItemType.CUSTOMIZE_SELECT,label: '状态',name: 'status',
  url: '/dictionary/getDicDataByGroupId?groupId=start_using',textFiled:'text',valueField: "val",defaultValue:'1'});
items.push({type: FormItemType.CUSTOMIZE_SELECT,label: '是否默认',name: 'defaultStatus',
  url: '/dictionary/getDicDataByGroupId?groupId=role_default_status',textFiled:'text',valueField: "val",defaultValue:'0'});
items.push({type: FormItemType.INPUT_TEXT,label: '描述',name: 'description'});
items.push({type: FormItemType.SLOT,label: '权限',name:'permissionTree'});

let roleForm = reactive({
  splitCol: 1,
  gutter: 20,
  labelWidth: 115,
  items:items,
  rules:{
    name: [
      { required: true, message: '请填写名称', trigger: 'blur' }
    ]
  },
});

const getMenuTreeData = () =>{
  menuTreeData.clear();
  $axios.post('/menu/getAllPermissionTree').then((response) => {
    const data = response.data;
    if(data.code = 1){
      let permissionData = data.data;
      menuTreeData.pushAll(permissionData);
    }
  },(error) => {
  });
}
getMenuTreeData();

const searchDicClick = () => {
  tableRef.value.reload(toRaw(searchFormData));
}

const resetSearchClick = () => {
  searchFormData.name = '';
}

const addDicClick = () => {
  roleEditFormRef.value.show();
}

const editTableData = (rowData,rowIndex) =>{
  roleEditFormRef.value.show();
  roleEditFormRef.value.setData(rowData);
  $axios.post("/role/getUserMenuByRoleId",{"id":rowData.id}).then(response =>{
    const data = response.data.data;
    let checkedArr = [];
    data.forEach(item => {
      checkedArr.push(item.id);
    });
    selectTree.value.setCheckedKeys(checkedArr);
  });
}
const deleteTableRowData = (rowData,rowIndex) => {
  deleteRow('/role/deleteRole',rowData,function (state,data){
      if(state == HandleEnum.SUCCESS){
        tableRef.value.reload();
      }
  });
}

const validateSuccess = (data) => {
  const menuIds = selectTree.value.getCheckedKeys();
  data.string1 = menuIds.join(',');
  $axios.post("/role/addOrUpdate",data).then(response => {
    const  data = response.data;
    ElMessage.info(data.msg);
    if(data.code == 1){
      tableRef.value.reload();
      roleEditFormRef.value.close();
    }
  },error => {

  });
}

</script>

<style scoped>

</style>